package com.kele.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kele.common.model.response.PageResult;
import com.kele.common.model.response.Result;
import com.kele.goods.mapper.BrandMapper;
import com.kele.model.goods.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class BrandService {

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    RedisTemplate redisTemplate;

    private final  String brandCache = "brandList";

    public Result insert(Brand brand){
        brandMapper.insert(brand);
        redisTemplate.delete(brandCache);
        return new Result(true,"1","新增品牌成功");
    }

    public Result updateById(Brand brand){
        brandMapper.updateById(brand);
        redisTemplate.delete(brandCache);
        return new Result(true,"1","修改品牌成功");
    }

    public Result deleteById(Long id){
        brandMapper.deleteById(id);
        redisTemplate.delete(brandCache);
        return new Result(true,"1","删除品牌成功");
    }

    public Result deleteBatchByIds(List<Long> ids){
        brandMapper.deleteBatchIds(ids);
        redisTemplate.delete(brandCache);
        return new Result(true,"1","删除品牌成功");
    }

    public Brand selectById(Long id){
        return brandMapper.selectById(id);
    }

    public List<Brand> findAll(){
        //如果Redis 有，从redis中获取，如果没有，从数据库查询，保存到redis中
        List<Brand>  brandListFromRedis = (List<Brand>)redisTemplate.opsForValue().get(brandCache);
        System.out.println("从Redis中获取");
        if(brandListFromRedis==null){
            System.out.println("Redis中为空");
            List<Brand> brandListFormDB =  brandMapper.selectList(Wrappers.emptyWrapper());;
            System.out.println("从数据库中查询");
            redisTemplate.opsForValue().set(brandCache,brandListFormDB);
            System.out.println("保存到Redis中");
            return brandListFormDB;
        }else {
            System.out.println("Redis中不为空，直接返回");
            return brandListFromRedis;
        }
    }

    /**
     *
     * @param pageNo
     * @param pageSize
     * @param searchMap
     * {
     *     "name":"",
     *     "firstChar":""
     * }
     * @return
     */
    public PageResult<Brand> findPage(Integer pageNo, Integer pageSize, Map searchMap){
        Page<Brand> page = new Page<>(pageNo,pageSize);

        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        String name = (String)searchMap.get("name");
        if(!StringUtils.isEmpty(name)){
            queryWrapper.lambda().like(Brand::getName,name);
        }
        String firstChar = (String)searchMap.get("firstChar");
        if(!StringUtils.isEmpty(firstChar)){
            queryWrapper.lambda().eq(Brand::getFirstChar,firstChar);
        }
        Page<Brand> page1 = brandMapper.selectPage(page, queryWrapper);

        return new PageResult<>(page1.getTotal(),page1.getRecords());
    }
}
